﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace PersonDetails
{
    public partial class frmPersonDetails : Form
    {
        string MyConnectionString = "Server=localhost;Database=mydb;Uid=root;Pwd=Kiran123";

        public frmPersonDetails()
        {
            InitializeComponent();
        }
        
        private void btnAddToData_Click(object sender, EventArgs e)
        {
            PersonDetails details = new PersonDetails();
            details.CustomerId = 10;
            details.FirstName = txtFirstName.Text;
            details.MiddleName = txtMiddleName.Text;
            details.LastName = txtLastName.Text;
            details.email = txtEmailId.Text;
            details.dateOfBirth = txtDOB.Text;
            details.addressid = 1;

            DbInteraction dbInteraction = new DbInteraction();

            dbInteraction.Insert(details);
        }

        private void loadData()
        {   
            using (MySqlConnection conn = new MySqlConnection(MyConnectionString))
            {
                conn.Open();
                MySqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM customer";

                MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
                DataSet dtSet = new DataSet();
                adapter.Fill(dtSet);
                dataGridView1.DataSource = dtSet.Tables[0].DefaultView;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                PersonDetails detail = new PersonDetails();
                int customerid;
                int.TryParse(Convert.ToString(row.Cells["Customer_ID"].Value), out customerid);
                detail.CustomerId = customerid;

                detail.FirstName = Convert.ToString(row.Cells["first_name"].Value);
                detail.MiddleName = Convert.ToString(row.Cells["middle_name"].Value);
                detail.LastName = Convert.ToString(row.Cells["last_name"].Value);
                detail.email = Convert.ToString(row.Cells["email"].Value);

                int addressid;
                int.TryParse(Convert.ToString(row.Cells["address_id"].Value), out addressid);
                detail.addressid = addressid;

                detail.dateOfBirth = Convert.ToString(row.Cells["DOB"].Value);

                DbInteraction dbinteract = new DbInteraction();
                dbinteract.Update(detail);

                DataSet dtSet = dbinteract.select();
                dataGridView1.DataSource = dtSet.Tables[0].DefaultView;
                
            }
        }

        private void btnDisplayData_Click(object sender, EventArgs e)
        {
            DataSet dtSet = new DataSet();
            DbInteraction dbInteraction = new DbInteraction();
            dtSet = dbInteraction.select();
            dataGridView1.DataSource = dtSet.Tables[0].DefaultView;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DbInteraction dbInteract = new DbInteraction();

            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                int custID ;
                int.TryParse(Convert.ToString(row.Cells["Customer_ID"].Value), out custID);
                dbInteract.Delete(custID);
            }

        }

    }
}
